'ui';
let isRunning = false
let wd = null

let date = new Date()
if (date.getUTCMonth() + 1 >= 8 && date.getDate() > 28) {
	toast('请付款后使用')
	exit()
}
// console.show()
// 密码多少位
let pwdCount = 6
// [0-9][a-z][A-Z]
let num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
let lower = [
	'a',
	'b',
	'c',
	'd',
	'e',
	'f',
	'g',
	'h',
	'i',
	'j',
	'k',
	'l',
	'm',
	'n',
	'o',
	'p',
	'q',
	'r',
	's',
	't',
	'u',
	'v',
	'w',
	'x',
	'y',
	'z',
]
let upper = lower.map(item => {
	return item.toUpperCase()
})
let res = num.concat(lower).concat(upper)

let storage = storages.create('yun')

let conf = storage.get('conf', {
	'3': '000',
	'4': '0000',
	'5': '00000',
	'6': '000000',
	'7': '0000000',
	'8': '00000000',
	'9': '000000000',
	'10': '0000000000',
	'11': '00000000000',
	'12': '000000000000',
})
let currntPwd = '000' //storage.get("currntPwd", '000000');
let pwd = ''
let resArr = []
let isContinue = false

let len = 3
let th
events.on('exit', function () {
	// storage.put('currntPwd', pwd)
	conf[len] = pwd
	storage.put('conf', conf)
})

threads.start(function () {
	// 检测是否打开无障碍权限
	if (!floaty.checkPermission()) {
		floaty.requestPermission()
		exit()
	}
	sleep(1000)
	wd = floaty.rawWindow(
		<vertical w="*" h="*">
			<frame layout_weight="1" h="30" layout_gravity="center">
				<img
					w="*"
					h="30"
					src="#ffffff"
					borderWidth="1dp"
					scaleType="fitXY"
					circle="true"
				/>
				<text
					id="start"
					h="30"
					text="开始运行"
					textSize="12sp"
					gravity="center"
					padding="15 0 "
				/>
			</frame>
			<frame h="30" layout_weight="1" layout_gravity="center" marginTop="2">
				<img
					w="*"
					h="30"
					src="#ffffff"
					borderWidth="1dp"
					scaleType="fitXY"
					circle="true"
				/>
				<text
					id="stop"
					h="30"
					text="结束运行"
					textSize="12sp"
					gravity="center"
					padding="15 0"
				/>
			</frame>
		</vertical>
	)
	wd.setPosition(40, device.height / 2)
	wd.exitOnClose()
	wd.stop.on('click', function () {
		isRunning = false
		exit()
		toastLog('运行结束')
	})
	wd.start.on('click', function () {
		//程序开始运行之前判断无障碍服务
		if (auto.service == null) {
			toast('请先开启无障碍服务！')
			return
		}

		if (isRunning) {
			toastLog('运行暂停')
			isRunning = false
			console.clear()
			console.hide()
			ui.run(function () {
				wd.start.setText('开始运行')
			})
			
			//threads.shutDownAll()
			return
		}

		let mPwd = ui.mPwd.text().toString()
		if (mPwd.length != len) {
			toastLog('密码长度不符')
			return
		}
		currntPwd = mPwd
		conf[len] = currntPwd
		isRunning = true
		ui.run(function () {
			wd.start.setText('暂停运行')
		})

		console.show()
		if (th == null) { 
			th = threads.start(function () {
				toastLog('运行开始')
				sleep(1000)
				// while (true) {
					try {
						main()
					} catch (error) {
						
					}
				// }
			})
		}

		
	})
})

ui.statusBarColor('#000000')
ui.layout(
	<vertical>
		<appbar bg="#000000">
			<toolbar title="云课" />
		</appbar>
		<Switch
			id="floatx"
			text="悬浮窗权限"
			checked="{{floaty.checkPermission()}}"
			padding="8 8 8 8"
			textSize="15sp"
			gravity="right"
		/>
		<Switch
			id="autoService"
			text="无障碍服务"
			checked="{{auto.service != null}}"
			padding="8 8 8 8"
			textSize="15sp"
			gravity="right"
		/>
		<horizontal gravity="center">
			<text textSize="12sp" gravity="center" text="密码长度：" />
			<spinner id="len" entries="3|4|5|6|7|8|9|10|11|12" />
			<input id="mPwd" text="" textSize="12sp" w="80" gravity="center" />
		</horizontal>
		<text
			textSize="12sp"
			gravity="center"
			text="(淘宝：保图工作室) 测试（8.22）"
		/>
	</vertical>
)
ui.len.setOnItemSelectedListener({
	onItemSelected: function (parent, view, position, id) {
		len = position + 3
		ui.run(function () {
			ui.mPwd.setText(conf[len])
		})
		if (th != null) { 
			th.interrupt()
			th = null
		}
	},
	onNothingSelected: function (parent) {},
})

ui.floatx.on('click', function (checked) {
	floaty.requestPermission()
})
ui.autoService.on('check', function (checked) {
	// 用户勾选无障碍服务的选项时，跳转到页面让用户去开启
	if (checked && auto.service == null) {
		app.startActivity({
			action: 'android.settings.ACCESSIBILITY_SETTINGS',
		})
	}
	if (!checked && auto.service != null) {
		auto.service.disableSelf()
	}
})

// 当用户回到本界面时，resume事件会被触发
ui.emitter.on('resume', function () {
	// 此时根据无障碍服务的开启情况，同步开关的状态
	ui.floatx.checked = floaty.checkPermission()
	ui.autoService.checked = auto.service != null
	// ui.mPwd.setText(currntPwd)
})
function getPwd3() {
	for ( ; i < res.length; i++) {
		for ( ; j < res.length; j++) {
			for ( ; m < res.length; m++) {
				resArr = [res[i], res[j], res[m]]
				pwd = resArr.join('')
				if (!isContinue) {
					isContinue = currntPwd == pwd
				} else {
					login(pwd)
				}
			}
			m = 0 
		}
		j = o
	}
	i = 0
}
function getPwd4() {
	for ( ; i < res.length; i++) {
		for ( ; j < res.length; j++) {
			for ( ; m < res.length; m++) {
				for ( ; n < res.length; n++) {
					resArr = [res[i], res[j], res[m], res[n]]
					pwd = resArr.join('')
					if (!isContinue) {
						isContinue = currntPwd == pwd
					} else {
						login(pwd)
					}
				}
				n=0
			}
			m=0
		}
		j =0
	}
	i = 0
}
function getPwd5() {
	for ( ; i < res.length; i++) {
		for ( ; j < res.length; j++) {
			for ( ; m < res.length; m++) {
				for ( ; n < res.length; n++) {
					for ( ; p < res.length; p++) {
						resArr = [res[i], res[j], res[m], res[n], res[p]]
						pwd = resArr.join('')
						if (!isContinue) {
							isContinue = currntPwd == pwd
						} else {
							login(pwd)
						}
					}
					p =0
				}
				n=0
			}
			m =0 
		}
		j = 0
	}
	i = 0 
}
function getPwd6() {
	for ( ; i < res.length; i++) {
		for ( ; j < res.length; j++) {
			for ( ; m < res.length; m++) {
				for ( ; n < res.length; n++) {
					for ( ; p < res.length; p++) {
						for ( ; q < res.length; q++) {
							resArr = [res[i], res[j], res[m], res[n], res[p], res[q]]
							pwd = resArr.join('')
							if (!isContinue) {
								isContinue = currntPwd == pwd
							} else {
								login(pwd)
							}
						}
						q = 0
					}
					p = 0 
				}
				n = 0
			}
			m =0 
		}
		j = 0 
	}
	i = 0
}
function getPwd7() {
	for ( ; i < res.length; i++) {
		for ( ; j < res.length; j++) {
			for ( ; m < res.length; m++) {
				for ( ; n < res.length; n++) {
					for ( ; p < res.length; p++) {
						for ( ; q < res.length; q++) {
							for ( ; x < res.length; x++) {
								resArr = [
									res[i],
									res[j],
									res[m],
									res[n],
									res[p],
									res[q],
									res[x],
								]
								pwd = resArr.join('')
								if (!isContinue) {
									isContinue = currntPwd == pwd
								} else {
									login(pwd)
								}
							}
							x = 0
						}
						q =0
					}
					p = 0
				}
				n= 0
			}
			m= 0
		}
		j= 0
	}
	i =0
}
function getPwd8() {
	for ( ; i < res.length; i++) {
		for ( ; j < res.length; j++) {
			for ( ; m < res.length; m++) {
				for ( ; n < res.length; n++) {
					for ( ; p < res.length; p++) {
						for ( ; q < res.length; q++) {
							for ( ; x < res.length; x++) {
								for ( ; y < res.length; y++) {
									resArr = [
										res[i],
										res[j],
										res[m],
										res[n],
										res[p],
										res[q],
										res[x],
										res[y],
									]
									pwd = resArr.join('')
									if (!isContinue) {
										isContinue = currntPwd == pwd
									} else {
										login(pwd)
									}
								}
								y =0 
							}
							x = 0
						}
						q =0
					}
					p = 0
				}
				n= 0
			}
			m= 0
		}
		j= 0
	}
	i =0
}
function getPwd9() {
	for ( ; i < res.length; i++) {
		for ( ; j < res.length; j++) {
			for ( ; m < res.length; m++) {
				for ( ; n < res.length; n++) {
					for ( ; p < res.length; p++) {
						for ( ; q < res.length; q++) {
							for ( ; x < res.length; x++) {
								for ( ; y < res.length; y++) {
									for ( ; r < res.length; r++) {
										resArr = [
											res[i],
											res[j],
											res[m],
											res[n],
											res[p],
											res[q],
											res[x],
											res[y],
											res[r],
										]
										pwd = resArr.join('')
										if (!isContinue) {
											isContinue = currntPwd == pwd
										} else {
											login(pwd)
										}
									}
									r = 0
								}
								y =0 
							}
							x = 0
						}
						q =0
					}
					p = 0
				}
				n= 0
			}
			m= 0
		}
		j= 0
	}
	i =0
}
function getPwd10() {
	for ( ; i < res.length; i++) {
		for ( ; j < res.length; j++) {
			for ( ; m < res.length; m++) {
				for ( ; n < res.length; n++) {
					for ( ; p < res.length; p++) {
						for ( ; q < res.length; q++) {
							for ( ; x < res.length; x++) {
								for ( ; y < res.length; y++) {
									for ( ; r < res.length; r++) {
										for ( ; z < res.length; z++) {
											resArr = [
												res[i],
												res[j],
												res[m],
												res[n],
												res[p],
												res[q],
												res[x],
												res[y],
												res[r],
												res[z],
											]
											pwd = resArr.join('')
											if (!isContinue) {
												isContinue = currntPwd == pwd
											} else {
												login(pwd)
											}
										}
										z = 0
									}
									r = 0
								}
								y =0 
							}
							x = 0
						}
						q =0
					}
					p = 0
				}
				n= 0
			}
			m= 0
		}
		j= 0
	}
	i =0
}
function getPwd11() {
	for ( ; i < res.length; i++) {
		for ( ; j < res.length; j++) {
			for ( ; m < res.length; m++) {
				for ( ; n < res.length; n++) {
					for ( ; p < res.length; p++) {
						for ( ; q < res.length; q++) {
							for ( ; x < res.length; x++) {
								for ( ; y < res.length; y++) {
									for ( ; r < res.length; r++) {
										for ( ; z < res.length; z++) {
											for ( ; c < res.length; c++) {
												resArr = [
													res[i],
													res[j],
													res[m],
													res[n],
													res[p],
													res[q],
													res[x],
													res[y],
													res[r],
													res[z],
													res[c],
												]
												pwd = resArr.join('')
												if (!isContinue) {
													isContinue = currntPwd == pwd
												} else {
													login(pwd)
												}
											}
											c = 0
										}
										z = 0
									}
									r = 0
								}
								y =0 
							}
							x = 0
						}
						q =0
					}
					p = 0
				}
				n= 0
			}
			m= 0
		}
		j= 0
	}
	i =0
}
function getPwd12() {
	for ( ; i < res.length; i++) {
		for ( ; j < res.length; j++) {
			for ( ; m < res.length; m++) {
				for ( ; n < res.length; n++) {
					for ( ; p < res.length; p++) {
						for ( ; q < res.length; q++) {
							for ( ; x < res.length; x++) {
								for ( ; y < res.length; y++) {
									for ( ; r < res.length; r++) {
										for ( ; z < res.length; z++) {
											for ( ; c < res.length; c++) {
												for ( ; d < res.length; d++) {
													resArr = [
														res[i],
														res[j],
														res[m],
														res[n],
														res[p],
														res[q],
														res[x],
														res[y],
														res[r],
														res[z],
														res[c],
														res[d],
													]
													pwd = resArr.join('')
													if (!isContinue) {
														isContinue = currntPwd == pwd
													} else {
														login(pwd)
													}
												}
												d = 0
											}
											c = 0
										}
										z = 0
									}
									r = 0
								}
								y =0 
							}
							x = 0
						}
						q =0
					}
					p = 0
				}
				n= 0
			}
			m= 0
		}
		j= 0
	}
	i =0
}
function getIndex(p) { 
	let resStr = res.join('')
	return resStr.indexOf(p)
}
let i = 0
let j = 0
let m = 0
let n = 0
let p = 0
let q = 0
let x = 0
let y = 0
let r = 0
let z = 0
let c = 0
let d = 0
function main() {
	let indexArr = []
	console.log('密码：' + currntPwd);
	for (let w = 0; w < currntPwd.length; w++) { 
		let p = currntPwd[w]
		indexArr[w] = getIndex(p)
	}

	i = indexArr[0] || 0
	j = indexArr[1] || 0
	m = indexArr[2] || 0
	n = indexArr[3] || 0
	p = indexArr[4] || 0
	q = indexArr[5] || 0
	x = indexArr[6] || 0
	y = indexArr[7] || 0
	r = indexArr[8] || 0
	z = indexArr[9] || 0
	c = indexArr[10] || 0
	d = indexArr[11] || 0

	if (len == 3) {
		getPwd3()
	} else if (len == 4) {
		getPwd4()
	} else if (len == 5) {
		getPwd5()
	} else if (len == 6) {
		getPwd6()
	} else if (len == 7) {
		getPwd7()
	} else if (len == 8) {
		getPwd8()
	} else if (len == 9) {
		getPwd9()
	} else if (len == 10) {
		getPwd10()
	} else if (len == 11) {
		getPwd11()
	} else if (len == 12) {
		getPwd12()
	}
}

function login(pwd) {
	while (!isRunning) {}
	try {
		console.log(pwd)
		// id('mEdtAccount').className('android.widget.EditText').findOne(2000).setText(account)
		id('mEdtPassword')
			.className('android.widget.EditText')
			.findOne(2000)
			.setText(pwd)
		id('mTvLogin').findOne(2000).click()
	} catch (error) {
		exit()
	}
}

// function permutations(string) {
//     return (string.length == 1) ? [string] : string.split('').map(
//        (e, i) => permutations(string.slice(0,i) + string.slice(i+1)).map((e2) => e+e2)
//     ).reduce((r,e) => r.concat(e)).sort().filter((e,i,a) => (i==0) || a[i-1] != e);
// }
// console.log(permutations('123456'));

// let pwd = ''
// for (let i = 0; i < pwdCount; i++) {
//     pwd += res[0]
// }
// console.log(pwd);

// function fullpermutate(str) {
// 	var result = []
// 	if (str.length > 1) {
// 		//遍历每一项
// 		for (var m = 0; m < str.length; m++) {
// 			//拿到当前的元素
// 			var left = str[m]
// 			//除当前元素的其他元素组合
// 			var rest = str.slice(0, m) + str.slice(m + 1, str.length)
// 			//上一次递归返回的全排列
//             var preResult = fullpermutate(rest)
//             console.log(preResult);
// 			//组合在一起
// 			for (var i = 0; i < preResult.length; i++) {
//                 var tmp = left + preResult[i]
// 				result.push(tmp)
// 			}
// 		}
//     } else if (str.length == 1) {

// 		result.push(str)
// 	}
// 	return result
// }

// fullpermutate('fdsda')
